<html>
<body>

<form action="calculator.php" method="post">
Expression: <input type="text" name="expr" />
<input type="submit" />
</form>

<?php
    //error_reporting(E_ERROR);

    $expr=$_POST["expr"];

    $number="[0-9]";
    $operator="[\+\-\*\/]";
    $period="\.";
    $modifier="\-";
    $valid_number="($modifier?$number*$period?$number+)";
    $pattern="/^($valid_number$operator)*$valid_number$/";
    $zero_division1="/\/0*$operator/";
    $zero_division2="/\/0*$/";
    $zero_division3="/\/0*\.0*$operator/";
    $zero_division4="/\/0*\.0*$/";
    if (preg_match($zero_division1, $expr)) echo "Invalid Expression: divided by 0";
    elseif (preg_match($zero_division2, $expr)) echo "Invalid Expression: divided by 0";
    elseif (preg_match($zero_division3, $expr)) echo "Invalid Expression: divided by 0";
    elseif (preg_match($zero_division4, $expr)) echo "Invalid Expression: divided by 0";
    elseif (preg_match($pattern, $expr)) eval("echo $expr;");
    else echo "Invalid Expression";
?>

</body>
</html> 
